const express = require("express");
const session = require("express-session");
const path = require("path");
const app = express();

// 引入拆分出去的路由模块
const userRouter = require("./routes/user");
const goodsRouter = require("./routes/goods");
const orderRouter = require("./routes/order");
const usersRouter = require('./routes/users')
app.use(express.static(path.resolve(__dirname, "./public")));

// req.body中间件的设置
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 设置session相关的中间件
app.use(
  session({
    // 选项配置
    resave: true,
    saveUninitialized: true,
    secret: "manage",
    cookie: {
      maxAge: 60 * 1000 * 2
    }
  })
);
app.all("*", function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild"
  );
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", " 3.2.1");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});
// app.use(auth);
// app.use("/posts", auth, postRouter);
app.use(userRouter);
app.use(goodsRouter);
app.use(orderRouter);
app.use(usersRouter)

app.listen(8080);
